package com.cskaoyan.javase.array._5operation._1get;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 参加快乐男声比赛,有10个评委打分,(去掉一个最高一个最低)求最后的平均成绩
 * 提示：用数组装这些分数，并找到最大值和最小值去掉他们，然后算平均值。
 * 要求：必须提供三个方法：求最大值，最小值，平均分
 *
 * @since 14:24
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo {
    public static void main(String[] args) {
        // 10个评委打分,就有十个成绩
        // 所以需要一个数组来存放这些成绩,由于需要计算平均分,用double类型数组存放
        double[] scores = new double[10];
        Scanner sc = new Scanner(System.in);
        // 用Scanner遍历进行赋值
        for (int i = 0; i < 10; i++) {
            System.out.print("请输入第" + (i + 1) + "位评委的成绩(十分制):");
            int grade = sc.nextInt();
            // 把成绩存入数组
            scores[i] = grade;
        }
        // scores = new double[]{7, 6, 7, 8, 10, 9, 8, 7, 6, 5};
        System.out.println(Arrays.toString(scores));

        double maxScore = getMaxScore(scores);
        double minScore = getMinScore(scores);
        System.out.println("评委打分的最高分是:" + maxScore);
        System.out.println("评委打分的最低分是:" + minScore);
        double total = getTotal(scores);
        System.out.println("评委打分的总分是:" + total);
        double ave = getAverage(total, maxScore, minScore);
        System.out.println("评委打分的平均分是:" + ave);

    }

    public static double getAverage(double total, double maxScore, double minScore) {
        return (total - maxScore - minScore) / 8;
    }

    public static double getTotal(double[] scores) {
        double sum = 0;
        for (double grade : scores) {
            sum += grade;
        }
        return sum;
    }

    /**
     * 获取double类型数组的最小值
     * @param scores 目标数组
     * @return double
     * @author wuguidong@cskaoyan.onaliyun.com
     * @since 2022/03/03 14:32
     */
    public static double getMinScore(double[] scores) {
        // 获取最值 一般是先假设首位元素是最值,然后遍历数组,比较
        double min = scores[0];
        for (double grade : scores) {
            if (grade < min) {
                min = grade;
            }
        }
        return min;
    }

    /**
     * 获取double类型数组的最大值
     * @param scores 目标数组
     * @return double
     * @author wuguidong@cskaoyan.onaliyun.com
     * @since 2022/03/03 14:32
     */
    public static double getMaxScore(double[] scores) {
        // 获取最值 一般是先假设首位元素是最值,然后遍历数组,比较
        double max = scores[0];
        for (double grade : scores) {
            if (grade > max) {
                max = grade;
            }
        }
        return max;
    }


}
